package com.sun.showcase.controller.mall;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

import com.sun.showcase.biz.mall.service.DistributionService;
import com.sun.showcase.pojo.DataGrid;
import com.sun.showcase.pojo.Result;
import com.sun.showcase.utils.LoginContextHolder;
import com.sun.showcase.client.domain.mall.Distribution;
import com.sun.showcase.client.query.mall.DistributionQuery;

@Controller
@RequestMapping(value="/mall/distribution") 
public class DistributionController{
	
	@Autowired
	private DistributionService distributionService;
	
	private Result result = new Result();
	/**
	 * 跳转到Distribution管理页面
	 * 
	 * @return
	 */
	@RequestMapping(value="goDistribution")
	public String goDistribution(ModelMap map) {
		map.put("shopId", LoginContextHolder.get().getShopId());
		map.put("maxLevel", 2);
		return "mall/distribution";
	}
	/**
	 * 跳转到Distribution新增页面
	 * 
	 * @return
	 */
	@RequestMapping(value="handleDistribution")
	public String handleDistribution(@RequestParam(value="id",required=false) Long id ,ModelMap map) {
		return "mall/distribution_rowEdit";
	}

	/**
	 * 跳转到查看desc页面
	 * 
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value="showDesc")
	public Distribution showDesc(DistributionQuery distributionQuery) {
		return distributionService.get(distributionQuery);
	}

	/**
	 * 获得分页数据表格
	 */
	@ResponseBody
	@RequestMapping(value = "/datagrid")
	public DataGrid datagrid(DistributionQuery distributionQuery) {
		return distributionService.datagrid(distributionQuery);
	}
	

	/**
	 * 添加一个Distribution
	 */
	@ResponseBody
	@RequestMapping(value="add")
	public Result add(DistributionQuery distributionQuery) {
		distributionService.add(distributionQuery);
		result.setSuccess(true);
		result.setMsg("添加成功！");
		return result;
	}

	/**
	 * 编辑Distribution
	 */
	@ResponseBody
	@RequestMapping(value="edit")
	public Result edit(DistributionQuery distributionQuery) {
		distributionService.update(distributionQuery);
		result.setSuccess(true);
		result.setMsg("编辑成功！");
		return result;
	}

	/**
	 * 物理删除Distribution
	 */
	@ResponseBody
	@RequestMapping(value="delete")
	public Result delete(DistributionQuery distributionQuery) {
		distributionService.delete(distributionQuery.getIds());
		result.setSuccess(true);
		result.setMsg("删除成功！");
		return result;
	}
	
	/**
	 * 逻辑删除Distribution
	 */
	@ResponseBody
	@RequestMapping(value="deletePt")
	public Result deletePt(DistributionQuery distributionQuery) {
		distributionService.deletePt(distributionQuery.getIds());
		result.setSuccess(true);
		result.setMsg("删除成功！");
		return result;
	}
	/**
	 * 全部数据Distribution
	 */
	@ResponseBody
	@RequestMapping(value="listAll")
	public Result listAll(DistributionQuery distributionQuery) {
		List<DistributionQuery> list = distributionService.listAll(distributionQuery);
		result.setSuccess(true);
		result.setMsg("成功！");
		result.setData(list);
		return result;
	}
	/**
	 * 全部数据Distribution
	 */
	@ResponseBody
	@RequestMapping(value="getNextLevel")
	public Result getNextLevel(String goodsId) {
		int level = distributionService.getNextLevel(goodsId);
		result.setSuccess(true);
		result.setMsg("成功！");
		result.setData(level);
		return result;
	}
}
